package com.android.danaobank.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.danaobank.R;
import com.android.danaobank.model.response.MiddleClass;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class MallIndicator extends LinearLayout implements View.OnClickListener {
    public MallIndicator(Context context) {
        super(context);
    }

    public MallIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MallIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private List<MiddleClass> middleClassList = new ArrayList<>();
    private List<View> itemViews = new ArrayList<>();

    public void setMiddleClassList(List<MiddleClass> middleClassList) {
        this.middleClassList.clear();
        this.middleClassList.addAll(middleClassList);
        initViews();
    }

    private int lastSelectedPosition;

    private void initViews() {
        removeAllViews();
        itemViews.clear();
        for (int i = 0; i < middleClassList.size(); i++) {
            MiddleClass middleClass = middleClassList.get(i);
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_mall_middle_class, null);
            TextView titleView = view.findViewById(R.id.tv_title);
            TextView subTitleView = view.findViewById(R.id.tv_subtitle);
            titleView.setText(middleClass.getName());
            subTitleView.setText(middleClass.getValue());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            addView(view, params);
            view.setTag(i);
            view.setOnClickListener(this);
            itemViews.add(view);
            if (i == 0) {
                titleView.setSelected(true);
                subTitleView.setSelected(true);
            }
            if (i != middleClassList.size() - 1) {
                View lineView = new View(getContext());
                params = new LinearLayout.LayoutParams(1, LayoutParams.MATCH_PARENT);
                params.topMargin = ConvertUtils.dp2px(10);
                params.bottomMargin = ConvertUtils.dp2px(10);
                params.leftMargin = ConvertUtils.dp2px(3);
                params.rightMargin = ConvertUtils.dp2px(3);
                lineView.setBackgroundColor(0x66666666);
                addView(lineView, params);
            }
        }
        lastSelectedPosition = 0;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (position == lastSelectedPosition) {
            return;
        }

        MiddleClass middleClass = middleClassList.get(position);
        ToastUtils.showLong("点击了：" + middleClass.getName());
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(position, middleClass);
        }

        itemViews.get(lastSelectedPosition).findViewById(R.id.tv_title).setSelected(false);
        itemViews.get(lastSelectedPosition).findViewById(R.id.tv_subtitle).setSelected(false);

        itemViews.get(position).findViewById(R.id.tv_title).setSelected(true);
        itemViews.get(position).findViewById(R.id.tv_subtitle).setSelected(true);

        lastSelectedPosition = position;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, MiddleClass middleClass);
    }
}
